<?php
namespace app\api\model;
use think\Db;
use traits\model\SoftDelete;

class SendCode extends Base
{
    use SoftDelete;
    // 设置当前模型的数据库连接
    public function make_token($user_id,$type){
           $token =  md5(md5(uniqid(microtime(true)).$user_id.sha1(OmSign)));
           Db::startTrans();
           $count = (new UserToken())->getCount([['user_id','eq',$user_id],['overDay_time','gt',time()]]);
           if($count>0){
               $res = (new UserToken())->fieldUpdate('delete_time',time(),"",[['user_id',"eq",$user_id]]);
               if(!$res){
                   Db::rollback();
                   return ['code'=>0,'msg'=>'token删除失败'];
               }
           }
           $new = [
               'user_id'=>$user_id,
               'token'  =>$token,
               'type'   =>$type,
               'overDay_time'=>time()+7*86400
           ];
           $res = (new UserToken())->dataUpdate($new);
           if(!$res){
               Db::rollback();
               return ['code'=>0,'msg'=>'token生成失败'];
           }else{
               Db::commit();
               return ['code'=>1,'msg'=>'token生成成功','token'=>$token];
           }
    }
}